﻿using System.Collections.Generic;
using DimOS.DimSH;
using DimOS.Internals;
using DateTime = DimOS.Plugs.DateTime;


namespace DimOS.DimFS
{
    public class Content
    {
        private List<Node> _children; 
        public List<Node> Children 
        {
            get { Accessed = DateTime.Now; return _children; }
            set { Accessed = Modified = DateTime.Now; _children = value; }
        }

        private byte[] _data;
        public byte[] Data
        { 
            get { Accessed = DateTime.Now; return _data; }
            set { Accessed = Modified = DateTime.Now; _data = value; }
        }
        

        internal DateTime Accessed { get; set; }

        internal DateTime Modified { get; set; }

        public Content(List<Node> children, byte[] data)
        {
            Children = (children == null && Settings.AdvancedPaths) ? new List<Node>() : children;
            Data = data ?? new byte[0];
        }

        internal int Size() { return Size(Children, Data) + (Data != null ? Data.Length : 0); }

        private int Size(List<Node> children, byte[] data)
        {
            if (children == null) return data != null ? data.Length : 0;
            int size = 0;
            for (int i = 0; i < children.Count; i++)
            {
                if (children[i].Content.Children == null)
                    size += children[i].Content.Data != null ? children[i].Content.Data.Length : 0;
                else
                {               
                    size += Size(children[i].Content.Children, children[i].Content.Data);
                    size += children[i].Content.Data != null ? children[i].Content.Data.Length : 0;
                }
            }
            return size;
        }
    }
}
